{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "# 添加项目目录到 Python 路径\n",
    "project_path = r\"D:\\code\\machine-learning\\逻辑回归\"\n",
    "if project_path not in sys.path:\n",
    "    sys.path.append(project_path)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "import my_logistic  # 首次导入\n",
    "# 修改 my_logistic.py 后，重新加载：\n",
    "from importlib import reload\n",
    "reload(my_logistic)  # 注意这里是模块对象，不是字符串\n",
    "import numpy as np\n",
    "import pandas as pd"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<>:1: SyntaxWarning: invalid escape sequence '\\c'\n",
      "<>:1: SyntaxWarning: invalid escape sequence '\\c'\n",
      "C:\\Users\\BLACK\\AppData\\Local\\Temp\\ipykernel_23796\\3054126724.py:1: SyntaxWarning: invalid escape sequence '\\c'\n",
      "  titanic_data = pd.read_csv('D:\\code\\python\\python-learning\\泰坦尼克\\data\\\\train.csv')\n",
      "C:\\Users\\BLACK\\AppData\\Local\\Temp\\ipykernel_23796\\3054126724.py:9: FutureWarning: DataFrame.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.\n",
      "  titanic_data = titanic_data.fillna(method=\"ffill\")\n"
     ]
    }
   ],
   "source": [
    "titanic_data = pd.read_csv('D:\\code\\python\\python-learning\\泰坦尼克\\data\\\\train.csv')\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "titanic_data[\"Name\"] = LabelEncoder().fit_transform(titanic_data[\"Name\"])\n",
    "titanic_data[\"Sex\"] = LabelEncoder().fit_transform(titanic_data[\"Sex\"])\n",
    "titanic_data[\"Cabin\"] = LabelEncoder().fit_transform(titanic_data[\"Cabin\"])\n",
    "titanic_data[\"Embarked\"] = LabelEncoder().fit_transform(titanic_data[\"Embarked\"])\n",
    "titanic_data[\"Ticket\"] = LabelEncoder().fit_transform(titanic_data[\"Ticket\"])\n",
    "\n",
    "titanic_data = titanic_data.fillna(method=\"ffill\")\n",
    "\n",
    "from  sklearn.model_selection import StratifiedShuffleSplit\n",
    "\n",
    "split = StratifiedShuffleSplit(n_splits=1, test_size=0.2)\n",
    "for train_indices,test_indices in split.split(titanic_data,titanic_data[[\"Survived\",\"Pclass\",\"Sex\"]]):\n",
    "    train_df = titanic_data.loc[train_indices]\n",
    "    test_df = titanic_data.loc[test_indices]\n",
    "\n",
    "X_train = train_df.drop(columns=\"Survived\").to_numpy()\n",
    "y_train = train_df[[\"Survived\"]].to_numpy()\n",
    "X_test = test_df.drop(columns=\"Survived\").to_numpy()\n",
    "y_test = test_df[[\"Survived\"]].to_numpy()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\code\\machine-learning\\逻辑回归\\my_logistic.py:20: RuntimeWarning: overflow encountered in exp\n",
      "  return 1 / (1 + np.exp(-z))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "L2正则正确率：\n",
      "0.38547847494003284\n",
      "L2正则测试正确率：\n",
      "0.37988826815642457\n",
      "L1正则正确率：\n",
      "0.3945366746622901\n",
      "L1正则测试正确率：\n",
      "0.3906245123435598\n",
      "myligbm正确率：\n",
      "0.6711731036732879\n",
      "myligbm测试正确率：\n",
      "0.6793770333673778\n",
      "ligbm正确率：\n",
      "0.8845810051005587\n",
      "ligbm测试正确率：\n",
      "0.825111123435618\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\BLACK\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\sklearn\\utils\\validation.py:1408: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n",
      "C:\\Users\\BLACK\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\sklearn\\linear_model\\_logistic.py:465: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
      "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
      "\n",
      "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
      "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
      "Please also refer to the documentation for alternative solver options:\n",
      "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
      "  n_iter_i = _check_optimize_result(\n",
      "C:\\Users\\BLACK\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\sklearn\\utils\\validation.py:1408: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
      "  y = column_or_1d(y, warn=True)\n",
      "C:\\Users\\BLACK\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages\\sklearn\\linear_model\\_sag.py:348: ConvergenceWarning: The max_iter was reached which means the coef_ did not converge\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "my_model =  my_logistic.MyLogisticRegression()\n",
    "my_model.fit(X_train, y_train)\n",
    "print(\"L2正则正确率：\")\n",
    "print(my_model.score(X_train, y_train))\n",
    "print(\"L2正则测试正确率：\")\n",
    "print(my_model.score(X_test, y_test))\n",
    "my_model.score(X_train, y_train)\n",
    "my_model =  my_logistic.MyLogisticRegression(penalty=\"l1\")\n",
    "my_model.fit(X_train, y_train)\n",
    "print(\"L1正则正确率：\")\n",
    "print(my_model.score(X_train, y_train))\n",
    "print(\"L1正则测试正确率：\")\n",
    "print(my_model.score(X_test, y_test))\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "sklearn_model = LogisticRegression()\n",
    "sklearn_model.fit(X_train, y_train)\n",
    "print(\"sklearn默认优化器正确率：\")\n",
    "print(sklearn_model.score(X_train, y_train))\n",
    "print(\"sklearn默认优化器测试正确率：\")\n",
    "print(sklearn_model.score(X_test, y_test))\n",
    "sklearn_model = LogisticRegression(solver=\"sag\")\n",
    "sklearn_model.fit(X_train, y_train)\n",
    "print(\"sklearn随机梯度下降优化器正确率：\")\n",
    "print(sklearn_model.score(X_train, y_train))\n",
    "print(\"sklearn随机梯度下降优化器测试正确率：\")\n",
    "print(sklearn_model.score(X_test, y_test))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}